class Sqlc < Formula
  desc "Generate type safe Go from SQL"
  homepage "https://sqlc.dev/"
  url "https://github.com/sqlc-dev/sqlc/archive/refs/tags/v1.30.0.tar.gz"
  sha256 "32a8ff2acd852c4a004383b441e6614b6c57ce1a294c0e455ab7431f017aa895"
  license "MIT"
  head "https://github.com/sqlc-dev/sqlc.git", branch: "main"

  bottle do
    sha256 cellar: :any_skip_relocation, arm64_tahoe:   "6fca7e95ad97972c257cd88e4ffd75cbc4fd466b0ee9fe9a7db21a9eb19b4d35"
    sha256 cellar: :any_skip_relocation, arm64_sequoia: "804df54f6aa327864d260e50070f44a1aa0d70e9983c069b3b08dac10a4b21d9"
    sha256 cellar: :any_skip_relocation, arm64_sonoma:  "72cf4daabe29f5ac51fe24d7fb511de87d672fe9e007f6e0de3191754376a649"
    sha256 cellar: :any_skip_relocation, arm64_ventura: "3a3400a7b98a53ab5b969fc4d7e30f4d7b1cd7e93da93cebc9aff9664690c465"
    sha256 cellar: :any_skip_relocation, sonoma:        "c88678492a2045628c286c57c17a2a29b213b5323fff965aabb6b3f0f47f976f"
    sha256 cellar: :any_skip_relocation, ventura:       "331997dac9ac8cd9bb2f6fecab3d867ec9c2b8aa9511fa768eff24dc44fb4834"
    sha256 cellar: :any_skip_relocation, arm64_linux:   "ee893ead14417e5cdfdeb738b797ac06d54624f278e98f31760ba1aeb603feeb"
    sha256 cellar: :any_skip_relocation, x86_64_linux:  "e823133161b575d2923432eb9bc5d52ef4143b20facdb7a3dd3aee2e067f1829"
  end

  depends_on "go" => :build

  def install
    system "go", "build", *std_go_args(ldflags: "-s -w"), "./cmd/sqlc"

    generate_completions_from_executable(bin/"sqlc", "completion")
  end

  test do
    (testpath/"sqlc.json").write <<~JSON
      {
        "version": "1",
        "packages": [
          {
            "name": "db",
            "path": ".",
            "queries": "query.sql",
            "schema": "query.sql",
            "engine": "postgresql"
          }
        ]
      }
    JSON

    (testpath/"query.sql").write <<~SQL
      CREATE TABLE foo (bar text);

      -- name: SelectFoo :many
      SELECT * FROM foo;
    SQL

    system bin/"sqlc", "generate"
    assert_path_exists testpath/"db.go"
    assert_path_exists testpath/"models.go"
    assert_match "// Code generated by sqlc. DO NOT EDIT.", File.read(testpath/"query.sql.go")
  end
end
